﻿using System;
using System.IO;
using System.Threading;
using System.Text;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.IO;

using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.System;

namespace FezPanda_SDCardTester
{
    public class SDCardModule
    {
        PersistentStorage sdPS = new PersistentStorage("SD");
        //public static void Main()
        //{
        //    // ... 
        //    // SD Card is inserted 
        //    // Create a new storage device 
        //    PersistentStorage sdPS = new PersistentStorage("SD");
        //    //FEZ_Shields.KeypadLCD.Initialize();
        //    //FEZ_Shields.KeypadLCD.TurnBacklightOn();
        //    //FEZ_Shields.KeypadLCD.SetCursor(0, 0);

        //    // Mount the file system 
        //    sdPS.MountFileSystem();   // Assume one storage device is available, access it through MF 
        //    WhatsOnSD(); //Outputs all the files and folders that are available on the SD card
        //    WritetoSD(); //Writes a string to .txt file specified in method 
        //    ReadfromSD(); //Reads first string from file specified in method and displays size of data being read



        //    // Unmount after all reading and writing completed
        //    sdPS.UnmountFileSystem();
        //}

        public void MountSD()
        {
            sdPS.MountFileSystem();
            Debug.Print("Mounted");
        }

        public void UnmountSD()
        {
            sdPS.UnmountFileSystem();
            Debug.Print("Unmounted");
        }


        public static void WhatsOnSD()
        {

            // Displays available files and folders: 
            Debug.Print("Getting files and folders:");
            if (VolumeInfo.GetVolumes()[0].IsFormatted)
            {
                //FEZ_Shields.KeypadLCD.Clear();
                string rootDirectory =
                     VolumeInfo.GetVolumes()[0].RootDirectory;
                string[] files = Directory.GetFiles(rootDirectory);
                string[] folders = Directory.GetDirectories(rootDirectory);

                Debug.Print("Files available on " + rootDirectory + ":");
                for (int i = 0; i < files.Length; i++)
                    Debug.Print(files[i]);

                Debug.Print("Folders available on " + rootDirectory + ":");
                for (int i = 0; i < folders.Length; i++)
                    Debug.Print(folders[i]);
            }
            else
            {
                Debug.Print("Storage is not formatted. Format on PC with FAT32/FAT16 first.");
            }
        }

        public void WriteJPEGtoSD(byte [] bytes)
        {
            FileStream FileHandle;

            //Debug.Print("Check 1");
            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;

            //Debug.Print("Check 2");

            if (File.Exists(rootDirectory + @"\test.jpeg"))
            {
                FileHandle = new FileStream(rootDirectory + @"\test.jpeg", FileMode.Append);
                FileHandle.Write(bytes, 0, bytes.Length);


            }
            else
            {
                FileHandle = new FileStream(rootDirectory + @"\test.jpeg", FileMode.Create);
                FileHandle.Write(bytes, 0, bytes.Length);
            }


             //Debug.Print("Check 3");

            FileHandle.Close();
            //Debug.Print("Check 4");
           // byte[] data = Encoding.UTF8.GetBytes("This string will go in the file!");
            // write the data and close the file 
            
           // FileHandle.Write(data, 0,data.Length);
            //Debug.Print("Check 5");
            
        }

        public static void WriteTesttoSD()
        {
            Debug.Print("Check 1");
            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;

            Debug.Print("Check 2");

            if(File.Exists(rootDirectory + @"\test.txt"))
            {
                File.Delete(rootDirectory + @"\test.txt");
            }

            Debug.Print("Check 3");

            FileStream FileHandle = new FileStream(rootDirectory + @"\test.txt", FileMode.Create);
            Debug.Print("Check 3");
            byte[] data = Encoding.UTF8.GetBytes("This string will go in the file!");
            // write the data and close the file 
            // FileHandle.Write(bytes, 0, bytes.Length);
            FileHandle.Write(data, 0, data.Length);
            Debug.Print("Check 4");
            FileHandle.Close();
        }
        public static void ReadfromSD()
        {

            // Assume one storage device is available, 
            // access it through NETMF 
            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
            FileStream FileHandle = new FileStream(rootDirectory + @"\hello.txt", FileMode.Open, FileAccess.Read);
            byte[] data = new byte[100];
            // write the data and close the file 
            int read_count = FileHandle.Read(data, 0, data.Length);
            FileHandle.Close();
            Debug.Print("The size of data we read is: " +
                          read_count.ToString());
            Debug.Print("Data from file:");
            Debug.Print(new string(Encoding.UTF8.GetChars(data), 0, read_count));

        }
    }

}